package zjl;

import java.util.List;

import zjl.graph.YenTopKShortestPathsAlgorithm;
import zjl.graph.Path;
import zjl.base.NodeHost;
import zjl.base.BizRequest;

public class RunRoutine {

    private ResourceStore resourceStore;

    public RunRoutine(String tfn, String bfn) {
        System.out.println("======================================");
        System.out.println("[STAGE_1]READ DATA FROM FILE...");
        System.out.println("======================================");
        this.resourceStore = new ResourceStore(tfn, bfn);
    }

    private void runKSPProcedure() {
        System.out.println("======================================");
        System.out.println("[STAGE_2]RUN TOPKSP COMPUTATION PROCESS.");
        System.out.println("======================================");
        YenTopKShortestPathsAlgorithm ytopkspa = new YenTopKShortestPathsAlgorithm(this.resourceStore);
        int reqSize = this.resourceStore.getBizRequestSize();
        for (int i = 0; i < reqSize; i++) {
            BizRequest request = this.resourceStore.getBizRequestAt(i);
            NodeHost begin = request.getSrc();
            NodeHost end = request.getDest();
            System.out.println("BEGIN = [" + begin.getName() + "] END = [" + end.getName() + "]");
            List<Path> kspaths = ytopkspa.getShortestPaths(begin, end, ConfigData.TOPK_VALUE);
            request.setKspPathList(kspaths);
            System.out.println("BUSINESS REQUEST[" + i + "] PATH_SIZE=" + kspaths.size());
        }
    }

    private void runGAProcedure() {
        System.out.println("======================================");
        System.out.println("[STAGE_3]RUN GENETIC ALGORITHM PROCESS.");
        System.out.println("======================================");
        GeneticAlgorithm ga = new GeneticAlgorithm(this.resourceStore);
        ga.executeGeneticProcess();
        ga.printEstimationResult();
    }

    private void runTTSProcedure() {
        System.out.println("======================================");
        System.out.println("[STAGE_4]RUN TIME TRIGGERED SCHEDULE PROCESS.");
        System.out.println("======================================");
    }

    public void run() {
        // this.runKSPProcedure();
        // this.runGAProcedure();
        // this.runTTSProcedure();
        fakeCorrectnessValidation();
    }

    public void fakeCorrectnessValidation() {
        System.out.println("Input: {");
        System.out.println("  <business>[src-dest]:length|period|hopdelay|e2edelay");
        System.out.println("  <b1>[h1-h12]:500KB|20ms|20ms|20ms");
        System.out.println("  <b2>[h1-h10]:100KB|40ms|40ms|40ms");
        System.out.println("  <b3>[h2-h5]:100KB|10ms|10ms|10ms");
        System.out.println("  <b4>[h2-h9]:200KB|20ms|20ms|20ms");
        System.out.println("}");

        System.out.println("Output: {");
        System.out.println("  PATH = {");
        System.out.println("    <business>:[pathnodes]");
        System.out.println("    <b1>:[h1-s1-s6-h12]");
        System.out.println("    <b2>:[h1-s4-s5-h10]");
        System.out.println("    <b3>:[h2-s2-s3-h5]");
        System.out.println("    <b4>:[h2-s2-s5-h9]");
        System.out.println("  }");
        System.out.println("  SCHED = {");
        System.out.println("    (port)<timepoint-business-duration-recv/send>...");
        System.out.println("    (h1p0)<0ms-b1-10ms-send><10ms-b2-20ms-send><30ms-b1-10ms-send>");
        System.out.println("    (h2p0)<0ms-b3-2ms-send><2ms-b4-4ms-send><10ms-b3-2ms-send>");
        System.out.println("    (h5p0)<0ms-b3-2ms-recv><10ms-b3-2ms-recv>");
        System.out.println("    (h9p0)<2ms-b4-2ms-recv>");
        System.out.println("    (h10p0)<10ms-b2-20ms-recv>");
        System.out.println("    (h12p0)<0ms-b1-10ms-recv><30ms-b1-10ms-recv>");
        System.out.println("    (s1p0)<0ms-b1-10ms-recv><10ms-b2-20ms-recv><30ms-b1-10ms-recv>");
        System.out.println("    (s1p1)<0ms-b3-2ms-recv><2ms-b4-4ms-recv><10ms-b3-2ms-recv>");
        System.out.println("    (s1p2)<0ms-b3-1ms-send><1ms-b4-2ms-send><10ms-b3-1ms-send>");
        System.out.println("    (s1p3)<10ms-b2-10ms-send>");
        System.out.println("    (s1p4)<0ms-b1-5ms-send><30ms-b1-5ms-send>");
        System.out.println("    (s2p0)<0ms-b3-1ms-recv><1ms-b4-2ms-recv><10ms-b3-1ms-recv>");
        System.out.println("    (s2p1)<0ms-b3-1ms-send><10ms-b3-1ms-send>");
        System.out.println("    (s2p2)<1ms-b4-2ms-send>");
        System.out.println("    (s3p0)<0ms-b3-1ms-recv><10ms-b3-1ms-recv>");
        System.out.println("    (s3p1)<0ms-b3-2ms-send><10ms-b3-2ms-send>");
        System.out.println("    (s4p0)<10ms-b2-10ms-recv>");
        System.out.println("    (s4p1)<10ms-b2-10ms-send>");
        System.out.println("    (s5p0)<1ms-b4-2ms-recv>");
        System.out.println("    (s5p1)<1ms-b4-4ms-send>");
        System.out.println("    (s5p2)<10ms-b2-10ms-recv>");
        System.out.println("    (s5p3)<10ms-b2-20ms-send>");
        System.out.println("    (s6p0)<0ms-b1-5ms-recv><30ms-b1-5ms-recv>");
        System.out.println("    (s6p1)<0ms-b1-10ms-send><30ms-b1-10ms-send>");
        System.out.println("  }");
        System.out.println("}");
    }
}
